package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDto;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    @Autowired
    private CheckItemService checkItemService;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = null;
        QueryWrapper<CheckGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        } else {
            //and 后做嵌套
           // where is_delete = 0 and (name like 条件 or HELPCODE like 条件)
            wrapper.and(i->i.like("name",queryPageBean.getQueryString()).or().like("HELPCODE", queryPageBean.getQueryString()));

            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public CheckGroupVO findCheckIteminfosByGroupId(int id) {
        int[] ids = baseMapper.findCheckItemIdsByGroupId(id);
        CheckGroupVO checkGroupVO = baseMapper.findCheckGroupById(id);
        List<CheckItem> list = checkItemService.list();
        checkGroupVO.setCheckItems(list);
        checkGroupVO.setCheckItemIds(ids);
        return checkGroupVO;
    }

    @Override
    public Boolean saveOrUpdatCheckGroup(CheckGroupDto checkGroupDto) {
        Integer gid = checkGroupDto.getId();
        if (gid != null) {
            //  根据 id  删除 中间表数据
            baseMapper.deleteCheckItemInfoByGroupId(gid);
        }
        //   完成检查组录入  自动生成主键
        boolean flag = saveOrUpdate(checkGroupDto);
        gid = checkGroupDto.getId();
        Integer[] itemIds = checkGroupDto.getItemIds();
        //  录入中间表
        for (Integer itemId : itemIds) {
            //   添加和修改  中间数据录入
            baseMapper.addCheckGroupIdAndCheckItemId(gid,itemId);
        }
        return flag;
    }


}
